package cn.wolfcode.wms.util;

import cn.wolfcode.wms.domain.Employee;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.Set;

public class UserContext {
	
	private UserContext() {}

	private static final String EMPLOYEE_IN_SESSION = "employee_in_session";
	private static final String EXPRESSIONS_IN_SESSION = "expressions_in_session";
	
	public static HttpSession getSession(){
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession();
	}
	
	//共享用户到session作用域
	public static void setUser(Employee currentUser){
		getSession().setAttribute(EMPLOYEE_IN_SESSION, currentUser);;
	}
	//从作用域中获取用户信息
	public static Employee getUser(){
		return (Employee) SecurityUtils.getSubject().getPrincipal();
		//return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
	}
	//共享用户权限到session作用域
	public static void setExpressions(Set<String> expressions){
		getSession().setAttribute(EXPRESSIONS_IN_SESSION, expressions);
	}
	//从作用域中获取用户权限信息
	public static Set<String> getExpressions(){
		return (Set<String>) getSession().getAttribute(EXPRESSIONS_IN_SESSION);
	}
	
}
